<template>
  <div class="cc-table">
    <div class="header">
      <slot name="header">
        <div class="title">{{ title }}</div>
        <div class="handler">
          <slot name="headerHandler"></slot>
        </div>
      </slot>
    </div>
    <el-table
      :data="listData"
      border
      style="width: 100%"
      @selection-change="selectionChange"
      :row-key="childrenProps?.rowKey"
      :tree-props="{ children: childrenProps?.treeProp?.children, hasChildren: childrenProps?.treeProp?.has_children }"
    >
      <el-table-column v-if="showSelectColumn" type="selection" align="center" width="60"></el-table-column>
      <!-- 这里的 type="index" 是 element-plus.table 自带的属性-->
      <el-table-column v-if="showIndexColumn" type="index" label="#" align="center" width="60"></el-table-column>
      <template v-for="item in propList" :key="item.prop">
        <!--
          <el-table-column :prop="item.prop" :label="item.label" :min-width="item.minWidth"></el-table-column>
          下面通过 v-bind 替代上面手动写属性的对应：:prop="item.prop"
          保证组件 props 中的属性名称和绑定的 item 即 propList 的属性一致即可
        -->
        <el-table-column v-bind="item" align="center" show-overflow-tooltip>
          <template #default="scope">
            <slot :name="item.slotName" :row="scope.row">
              {{ scope.row[item.prop] }}
            </slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div class="footer">
      <slot name="footer">
        <!--
          下面一行是组件 el-pagination 的一个属性
          v-model:currentPage="currentPage4"
          -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.currentPage"
          :page-size="page.pageSize"
          :page-sizes="[10, 20, 30, 40]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listCount"
        ></el-pagination>
      </slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import { ElTable, ElTableColumn, ElButton, ElPagination } from 'element-plus'

export default defineComponent({
  name: 'CcTable',
  components: { ElTable, ElTableColumn, ElButton, ElPagination },
  props: {
    title: {
      type: String,
      default: ''
    },
    listData: {
      type: Array,
      required: true
    },
    listCount: {
      type: Number,
      default: 0
    },
    propList: {
      type: Array,
      required: true
    },
    showIndexColumn: {
      type: Boolean,
      default: true
    },
    showSelectColumn: {
      type: Boolean,
      default: true
    },
    page: {
      type: Object,
      default: () => ({ currentPage: 0, pageSize: 10 })
    },
    childrenProps: {
      type: Object,
      default: () => ({})
    }
  },
  emits: ['selectionChange', 'update:page'],
  setup(props, { emit }) {
    console.log('ccTable接受的参数 props:', props.childrenProps)
    const selectionChange = (value: any) => {
      emit('selectionChange', value)
    }

    const handleCurrentChange = (currentPage: number) => {
      emit('update:page', { ...props.page, currentPage })
    }

    const handleSizeChange = (pageSize: number) => {
      emit('update:page', { ...props.page, pageSize })
    }

    return { selectionChange, handleSizeChange, handleCurrentChange }
  }
})
</script>
<style scoped lang="less">
.header {
  display: flex;
  height: 45px;
  padding: 0 5px;
  justify-content: space-between;
  align-items: center;

  .title {
    font-size: 20px;
    font-weight: 700;
  }

  .handler {
    align-items: center;
  }
}

.footer {
  margin-top: 15px;
  .el-pagination {
    text-align: right;
  }
}
</style>
